package com.geostar.constructionland.analyse.service.adjust;


import com.geostar.constructionland.analyse.agri.AdjustAgri;
import com.geostar.constructionland.common.entity.response.LandInfo;
import com.geostar.constructionland.common.entity.response.model.AdjustModel;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yelll
 * @since 2020-5-25 17:14
 * 通用的平差算法
 **/
@Component
public class NormalAdjustAgr implements IAdjust{
    @Override
    public List<LandInfo> adjust(AdjustModel adjustModel)throws Exception {
        BigDecimal sub_mj = new BigDecimal(adjustModel.getAdjustTotal());
        List<LandInfo> landInfos = adjustModel.getLandInfos();
        double adjustUnit = adjustModel.getAdjustAgrVersion().getAdjustUnit();
        if (sub_mj.compareTo(new BigDecimal(0)) > 0) {
            /**按照原始占压面积从大到小**/
            landInfos = landInfos.stream().sorted(Comparator.comparing(LandInfo::getZymj_ys, Comparator.reverseOrder())
                    .thenComparing(LandInfo::getDisBtwZymjDis, Comparator.reverseOrder())).collect(Collectors.toList());
        } else {
            /**按照原始地块面积从小到大**/
            landInfos = landInfos.stream().sorted(Comparator.comparing(LandInfo::getZymj_ys, Comparator.reverseOrder())
                    .thenComparing(LandInfo::getDisBtwZymjDis)).collect(Collectors.toList());
            /**设置平差单位**/
            adjustUnit = adjustUnit * -1;
        }
        /**平差总值**/
        double adjustTotal_abs = Math.abs(sub_mj.intValue());
        /**执行平差**/
        landInfos = AdjustAgri.adjust(landInfos, adjustTotal_abs, adjustUnit);
        return landInfos;
    }
}
